//
//  GatheringViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "GatheringViewController.h"
#import "Masonry.h"
#import "CIImage+Extension.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "XMRegisterViewController.h"

@interface GatheringViewController ()<UIPreviewInteractionDelegate>

@property (nonatomic,strong)UIView * bjTopView;
@property (nonatomic,strong)UIView * bjBottomView;
@property (nonatomic,strong)UIImageView * erweimaImage;
@property (nonatomic,strong)UIButton * yanjingButton;
@property (nonatomic,strong)UILabel * dizhiLabel;
@property (nonatomic,strong)NSString * address;
@property (nonatomic,strong)NSString *qbdzArr;

@end

@implementation GatheringViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:70/255.0f green:85/255.0f blue:254/255.0f alpha:1];
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"接收";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:titleLabel];
    
    UIButton * popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [popButton setImage:[UIImage imageNamed:@"icon_fanhui_b"] forState:UIControlStateNormal];
    [self.view addSubview:popButton];
    
    if (iPhoneX) {
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.view).with.offset(40);
    
            
        }];
        
        
        [popButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(40);
            make.left.equalTo(self.view).with.offset(20);
            make.width.equalTo(@40);
        }];
        
        
    }else {
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.view).with.offset(25);
            
        }];
        
        
        [popButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(25);
            make.left.equalTo(self.view).with.offset(20);
             make.width.equalTo(@40);
        }];
        
    }
    [popButton addTarget:self action:@selector(popButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self loadWangluoView];
    
}

- (void)loadWangluoView{
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{@"userId" : @(user)};
    [GPosAPI requestGetAccountAddressWithParams:params andCallback:^(id obj) {
        NSLog(@"dizhi---%@",obj);
        if ( obj == nil ){
            
            
        }else {
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                self.address = [[obj objectForKey:@"data"] objectForKey:@"address"];
            
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else {
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }
        }
        [self erweimaBJLoadView];
        [self creatCodeImage];
    }];
    
}

- (void)erweimaBJLoadView {
    self.bjTopView = [[UIView alloc] init];
    self.bjTopView.backgroundColor =[UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.bjTopView.layer.cornerRadius = 10;
    self.bjTopView.layer.masksToBounds = YES;
    [self.view addSubview:self.bjTopView];
    
    self.bjBottomView = [[UIView alloc] init];
    self.bjBottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bjBottomView];
    
    UIView * topImageView = [[UIView alloc] init];
    topImageView.backgroundColor = [UIColor clearColor];
    [self.bjTopView addSubview:topImageView];
    
    UIImageView *topimage = [[UIImageView alloc] init];
    [topImageView addSubview:topimage];
    
    UILabel * topLabel = [[UILabel alloc] init];
    topLabel.text = @"二维码接收";
    topLabel.font = [UIFont systemFontOfSize:14];
    topLabel.textColor = [UIColor colorWithRed:70/255.0f green:85/255.0f blue:254/255.0f alpha:1];
    [topImageView addSubview:topLabel];
    
    UILabel * tishiLabel = [[UILabel alloc] init];
    tishiLabel.text = @"扫二维码向我发送";
    tishiLabel.font = [UIFont systemFontOfSize:14];
    tishiLabel.textColor = [UIColor grayColor];
    [self.bjBottomView addSubview:tishiLabel];
    
    self.erweimaImage = [[UIImageView alloc] init];
    //    self.erweimaImage.image = [UIImage imageNamed:@"icon_ewm"];
    [self.bjBottomView addSubview:self.erweimaImage];
    
    UIButton * bcimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [bcimageButton setTitle:@"保存图片" forState:UIControlStateNormal];
    [bcimageButton setTitleColor:[UIColor colorWithRed:70/255.0f green:85/255.0f blue:254/255.0f alpha:1] forState:UIControlStateNormal];
    bcimageButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.bjBottomView addSubview:bcimageButton];
    [bcimageButton addTarget:self action:@selector(bcimageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel * qianbaoLabel = [[UILabel alloc] init];
    qianbaoLabel.text = @"钱包地址";
    qianbaoLabel.font = [UIFont systemFontOfSize:14];
    qianbaoLabel.textColor = [UIColor whiteColor];
    [self.bjBottomView addSubview:qianbaoLabel];
    
    self.yanjingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.yanjingButton setImage:[UIImage imageNamed:@"icon_biyan_b"] forState:UIControlStateNormal];
    [self.yanjingButton addTarget:self action:@selector(yangjingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //icon_yangjing_b
    [self.view addSubview:self.yanjingButton];
    
    self.dizhiLabel = [[UILabel alloc] init];
    self.dizhiLabel.font = [UIFont systemFontOfSize:14];
    self.dizhiLabel.numberOfLines = 2;
    self.dizhiLabel.textAlignment=NSTextAlignmentCenter;
    self.dizhiLabel.textColor = [UIColor whiteColor];
    [self.bjBottomView addSubview:self.dizhiLabel];
    
    
    UIButton * copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [copyButton setTitle:@"复制链接" forState:UIControlStateNormal];
    [copyButton setTitleColor:[UIColor colorWithRed:70/255.0f green:85/255.0f blue:254/255.0f alpha:1] forState:UIControlStateNormal];
    copyButton.titleLabel.font = [UIFont systemFontOfSize:14];
    copyButton.backgroundColor = [UIColor whiteColor];
    copyButton.layer.cornerRadius = 5;
    [self.view addSubview:copyButton];
    [copyButton addTarget:self action:@selector(copyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    if (IS_IPHONE5) {
        [self.bjTopView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(85);
            make.centerX.equalTo(self.view);
            make.left.equalTo(self.view).with.offset(30);
            make.right.equalTo(self.view).with.offset(-30);
            make.height.equalTo(@52);
        }];
        
        [self.bjBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bjTopView.mas_bottom).with.offset(-10);
            make.centerX.equalTo(self.view);
            make.left.equalTo(self.view).with.offset(30);
            make.right.equalTo(self.view).with.offset(-30);
            make.height.equalTo(@276);
            
        }];
        
        [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bjTopView).with.offset(1);
            make.centerX.equalTo(self.bjTopView);
            make.width.equalTo(@90);
            make.height.equalTo(@40);
        }];
        
        [tishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bjBottomView);
            make.top.equalTo(self.bjBottomView).with.offset(27);
        }];
        
        [self.erweimaImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tishiLabel.mas_bottom).with.offset(20);
            make.centerX.equalTo(self.bjBottomView);
            make.width.equalTo(@131);
            make.height.equalTo(@131);
            
        }];
        
        [bcimageButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.erweimaImage.mas_bottom).with.offset(30);
            make.centerX.equalTo(self.bjBottomView);
            
        }];
        
        
    }else {
        
        [self.bjTopView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(110);
            make.centerX.equalTo(self.view);
            make.left.equalTo(self.view).with.offset(34);
            make.right.equalTo(self.view).with.offset(-34);
            make.height.equalTo(@65);
        }];
        
        [self.bjBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bjTopView.mas_bottom).with.offset(-10);
            make.centerX.equalTo(self.view);
            make.left.equalTo(self.view).with.offset(34);
            make.right.equalTo(self.view).with.offset(-34);
            make.height.equalTo(@358);
            
            
        }];
        
        [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bjTopView).with.offset(2.5);
            make.centerX.equalTo(self.bjTopView);
            make.width.equalTo(@90);
            make.height.equalTo(@50);
            
        }];
        
        [tishiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bjBottomView);
            make.top.equalTo(self.bjBottomView).with.offset(35);
        }];
        
        [self.erweimaImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tishiLabel.mas_bottom).with.offset(24);
            make.centerX.equalTo(self.bjBottomView);
            make.width.equalTo(@170);
            make.height.equalTo(@170);
        }];
        
        [bcimageButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.erweimaImage.mas_bottom).with.offset(40);
            make.centerX.equalTo(self.bjBottomView);
            
        }];
        
        
    }
    [topimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topImageView);
        make.left.equalTo(topImageView).with.offset(0);
    }];
    topimage.image = [UIImage imageNamed:@"icon_sk_l"];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topImageView);
        make.left.equalTo(topimage.mas_right).with.offset(7);
    }];
    
    [qianbaoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bjBottomView.mas_bottom).with.offset(20);
        make.left.equalTo(self.view).with.offset(20);
        
    }];
    
    [self.yanjingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(qianbaoLabel);
        make.right.equalTo(self.view).with.offset(-20);
    }];
    [self.dizhiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(90);
        make.right.equalTo(self.view).with.offset(-55);
        make.centerY.equalTo(qianbaoLabel);
    }];
    
    [copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
        make.top.equalTo(qianbaoLabel.mas_bottom).with.offset(20);
        make.height.equalTo(@40);
    }];
    NSString * str = [self.address substringToIndex:4];
    NSString * twoStr = [self.address substringFromIndex:self.address.length-4];
    NSLog(@"str---%@, two--%@",str,twoStr);
    self.qbdzArr = [NSString stringWithFormat:@"%@****%@",str,twoStr];
    self.dizhiLabel.text = self.qbdzArr;
    
}
- (void)creatCodeImage{
    
    /**
     *  2.生成CIFilter(滤镜)对象
     *///
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    /**CIQRCodeGenerator
     *  3.恢复滤镜默认设置
     */
    [filter setDefaults];
    
    /**
     *  4.设置数据(通过滤镜对象的KVC)
     */
    //存放的信息
    NSString *info = self.address;
    //把信息转化为NSData
    NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding];
    //滤镜对象kvc存值
    [filter setValue:infoData forKeyPath:@"inputMessage"];
    
    /**
     *  5.生成二维码
     */
    CIImage *outImage = [filter outputImage];
    
    //imageView.image = [UIImage imageWithCIImage:outImage];//不处理图片模糊,故而调用下面的信息
    
    self.erweimaImage.image = [outImage createNonInterpolatedWithSize:150];
//        self.erweimaImage.image = [UIImage imageNamed:outImage];
    
}

- (void)ewmshoukuanBottomLoadView {
    UIView * bjBottomView = [[UIView alloc] init];
    bjBottomView.backgroundColor = [UIColor whiteColor];
    bjBottomView.layer.cornerRadius = 10;
    [self.view addSubview:bjBottomView];
    //    bjBottomView.hidden = YES;
    
    UIView * skView = [[UIView alloc] init];
    skView.backgroundColor = [UIColor clearColor];
    [bjBottomView addSubview:skView];
    
    UIView * xqView = [[UIView alloc] init];
    xqView.backgroundColor = [UIColor clearColor];
    [bjBottomView addSubview:xqView];
    
    UIView * View = [[UIView alloc] init];
    View.backgroundColor = [UIColor grayColor];
    [bjBottomView addSubview:View];
    
    if (IS_IPHONE5) {
        [bjBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bjBottomView.mas_bottom).with.offset(13);
            make.left.equalTo(self.view).with.offset(30);
            make.right.equalTo(self.view).with.offset(-30);
            make.height.equalTo(@81);
        }];
        
        [skView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bjBottomView).with.offset(0);
            make.left.equalTo(bjBottomView).with.offset(0);
            make.right.equalTo(bjBottomView).with.offset(0);
            make.height.equalTo(@38);
        }];
        
        
        
    }else {
        [bjBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bjBottomView.mas_bottom).with.offset(13);
            make.left.equalTo(self.view).with.offset(34);
            make.right.equalTo(self.view).with.offset(-34);
            make.height.equalTo(@105);
        }];
        
        [skView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bjBottomView).with.offset(0);
            make.left.equalTo(bjBottomView).with.offset(0);
            make.right.equalTo(bjBottomView).with.offset(0);
            make.height.equalTo(@50);
        }];
        
    }
    [View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(skView.mas_bottom).with.offset(0);
        make.left.equalTo(bjBottomView).with.offset(30);
        make.right.equalTo(bjBottomView).with.offset(-30);
        make.height.equalTo(@0.5);
    }];
    [xqView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(View).with.offset(0);
        make.left.equalTo(bjBottomView).with.offset(0);
        make.right.equalTo(bjBottomView).with.offset(0);
        make.bottom.equalTo(bjBottomView).with.offset(0);
    }];
    
    UILabel * shoukuanlabel = [[UILabel alloc] init];
    shoukuanlabel.text = @"收款";
    shoukuanlabel.font = [UIFont systemFontOfSize:14];
    [skView addSubview:shoukuanlabel];
    [shoukuanlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(skView).with.offset(30);
        make.centerY.equalTo(skView);
        make.width.equalTo(@40);
    }];
    
    UILabel * skjelabel = [[UILabel alloc] init];
    skjelabel.text = @"100GPOS";
    skjelabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    [skView addSubview:skjelabel];
    [skjelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(skView).with.offset(-30);
        make.centerY.equalTo(skView);
        
    }];
    
    UILabel * gjLabel = [[UILabel alloc] init];
    gjLabel.text = @"共计：";
    gjLabel.font = [UIFont systemFontOfSize:14];
    [skView addSubview:gjLabel];
    [gjLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(skjelabel.mas_left).with.offset(-5);
        make.centerY.equalTo(skView);
        make.width.equalTo(@45);
    }];
    
    UIImageView * xqimage = [[UIImageView alloc] init];
    [xqView addSubview:xqimage];
    [xqimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(xqView).with.offset(30);
        make.centerY.equalTo(xqView);
        make.width.equalTo(@30);
        make.height.equalTo(@30);
    }];
    xqimage.image = [UIImage imageNamed:@"icon_tx_mr"];
    
    UILabel * xqLabel = [[UILabel alloc] init];
    xqLabel.text = @"+100GPOS";
    xqLabel.font = [UIFont systemFontOfSize:14];
    [xqView addSubview:xqLabel];
    [xqLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(xqView).with.offset(-30);
        make.centerY.equalTo(xqView);
    }];
    
}

//保存图片
- (void)bcimageButtonAction:(UIButton *)sender {
    
    NSLog(@"--baocun--");
    [self makeScreenShotCompletion:^(UIImage *image) {
//
    }];

}

//眼睛
- (void)yangjingButtonAction:(UIButton *)sender {
    
    if([self.dizhiLabel.text rangeOfString:@"****"].location !=NSNotFound)
    {
        NSLog(@"yes");
        [self.yanjingButton setImage:[UIImage imageNamed:@"icon_yangjing_b"] forState:UIControlStateNormal];
        self.dizhiLabel.text = self.address;
    }
    else
    {
        
        [self.yanjingButton setImage:[UIImage imageNamed:@"icon_biyan_b"] forState:UIControlStateNormal];
        self.dizhiLabel.text = self.qbdzArr;
        NSLog(@"no");
    }
//    [self.yanjingButton setImage:[UIImage imageNamed:@"icon_biyan_b"]
    
    
}

//复制地址
- (void)copyButtonAction:(UIButton *)sender {
    NSLog(@"copy");
    UIPasteboard * pastboard = [UIPasteboard generalPasteboard];

    pastboard.string = self.address;

    [SVProgressHUD showSuccessWithStatus:@"复制钱包地址成功"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
//    [LCAlert toastShow:@"扫描结果已复制到剪贴板"];
    
}

-(void)makeScreenShotCompletion:(void(^)(UIImage * image))completion{
    //开启上下文  <span style="font-family: Arial, Helvetica, sans-serif;">设置截屏大小</span>
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    //获取图片
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    completion(image);
    
    /**
     *  将图片保存到本地相册
     */
    UIImageWriteToSavedPhotosAlbum(image, self , @selector(saveImage:didFinishSavingWithError:contextInfo:), nil);//保存图片到照片库
    
}
- (void)saveImage:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error == nil) {
        [self showAlertViewWithTitle:@"提示" withMessage:@"已经存入手机相册"];
    }
    else
    {
        [self showAlertViewWithTitle:@"提示" withMessage:@"保存失败"];
    }
}
#pragma mark ===========添加提示框===========
//提示框alert
- (void)showAlertViewWithTitle:(NSString *)aTitle withMessage:(NSString *)aMessage
{
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:aTitle message:[NSString stringWithFormat:@"%@",aMessage] preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [self presentViewController:alert animated:true completion:^{
        
    }];
    
}

- (void)popButtonAction:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
